package servlet;

import controller.ControllerProfiloLocal;
import controller.ControllerUtentiLocal;
import eccezioni.CampoErratoException;
import entity.UtenteRegistrato;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.Utilities;

/**
 * Servlet per mostrare il profilo di un utente e permettere di modificarne 
 * alcune informazioni.
 * @author Ale
 */
public class ProfiloServlet extends HttpServlet {
    @EJB
    private ControllerProfiloLocal controllerProfilo;

    @EJB
    private ControllerUtentiLocal controllerUtenti;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            Utilities.errore("Nessuna action specificata alla ProfiloServlet", request, response);
        } else if (action.equals("mostraProfilo")) {
            mostraProfilo(request, response);
        } else if (action.equals("mostraModificaInfoProfiloPopup")) { // richiesta ajax
            mostraModificaInfoProfiloPopup(request, response);
        } else if (action.equals("modificaInfoProfilo")) {
            modificaInfoProfilo(request, response);
        }

    }

    private void mostraProfilo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Long idUtente = Long.parseLong(request.getParameter("utente"));
        request.setAttribute("utente", controllerUtenti.getUtenteById(idUtente));
        Utilities.forward("/mostraProfilo.jsp", request, response);
    }

    private void mostraModificaInfoProfiloPopup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/WEB-INF/jspf/modificaInfoProfilo.jsp", request, response);
    }

    /* ciò che è corretto viene modificato */
    private void modificaInfoProfilo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String indirizzo = request.getParameter("indirizzo");
        String telefono = request.getParameter("telefono");
        String paypal = request.getParameter("paypal");
        String errore = "";
        try {
            controllerProfilo.modificaIndirizzo(Utilities.getUtenteLoggato(request), indirizzo);
        } catch (CampoErratoException cee) {
            errore += cee.getMessage();
        }
        try {
            controllerProfilo.modificaTelefono(Utilities.getUtenteLoggato(request), telefono);
        } catch (CampoErratoException cee) {
            errore += cee.getMessage();
        }
        try {
            controllerProfilo.modificaPaypal(Utilities.getUtenteLoggato(request), paypal);
        } catch (CampoErratoException cee) {
            errore += cee.getMessage();
        }

        if (!"".equals(errore)) {
            request.setAttribute("errore", errore);
        }
        UtenteRegistrato ur = Utilities.getUtenteLoggato(request);
        request.setAttribute("utente", ur);
        Utilities.forward("/mostraProfilo.jsp", request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
}
